package com.example.newstest.comment;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.newstest.R;
import com.example.newstest.BR;
import com.example.newstest.callback.OnCommentDataSuccessListener;
import com.example.newstest.comment.bean.CommentEntity;
import com.example.newstest.databinding.FragmentDayCommentBinding;
import com.example.newstest.until.CommentRecyclerAdapter;
import com.example.newstest.until.TaskCommentData;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class DayCommentFragment extends Fragment implements OnCommentDataSuccessListener {
    private List<CommentEntity> list;
    private FragmentDayCommentBinding binding;
    private int day;
    private CommentRecyclerAdapter<Object> adapter;

    public static DayCommentFragment newInstance(int  id) {
        Bundle args = new Bundle();
        args.putInt("id",id);
        DayCommentFragment fragment = new DayCommentFragment();
        fragment.setArguments(args);
        return fragment;
    }
    public DayCommentFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return DataBindingUtil.inflate(inflater, R.layout.fragment_day_comment, container, false).getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding = DataBindingUtil.getBinding(view);
        day =getArguments().getInt("id",0);
        String url = "http://m.mydrivers.com/app/HotComments.aspx?day="+day;
        adapter = new CommentRecyclerAdapter<>(getContext(), new ArrayList<>(), R.layout.comment_item, BR.commentEntity);
        binding.recycler.setAdapter(adapter);
        new TaskCommentData(this).execute(url);
    }


    @Override
    public void onCommentDataSuccess(List<CommentEntity> list) {
        adapter.addAll(list);
    }
}
